package cn.wolfcode.trip.base.util;

import cn.wolfcode.trip.base.domain.SendVerifyCodeVo;
import cn.wolfcode.trip.base.domain.User;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpSession;

//拿到用户信息的工具类
public abstract class UserContext {

    //将共享数据定义为一个静态常量,避免出错
    public static final String user_IN_SESSION="USER_IN_SESSION";
    public static final String VERIFY_CODE_IN_SESSION ="verify_code_in_session";
    //获取session
    //私有化,外部不需要调用
    public static HttpSession getSession(){
        return ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest().getSession();
    }

    //设置共享数据
    public static void setCurrentUser(User user){
        getSession().setAttribute("USER_IN_SESSION",user);
    }

    //设置共享数据
    public static User getCurrentUser(){
        return (User) getSession().getAttribute("USER_IN_SESSION");
    }

    //设置验证码发送记录到session
    public static void setVerifyCodeVo(SendVerifyCodeVo vo){
        getSession().setAttribute(VERIFY_CODE_IN_SESSION,vo);
    }
    //获取验证码发送记录从session
    public static SendVerifyCodeVo getVerifyCodeVo(){
        return (SendVerifyCodeVo) getSession().getAttribute(VERIFY_CODE_IN_SESSION);
    }
}
